﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
//using KingRoad.EPM.InvestmeEntity;
using System.Data;
using NBear.Mapping;
using DotNet.Utilities;
using DotNet.Model;
using KingRoad.EPM.Web.Util;
using DotNet.Service;
using DotNet.Model.MyEntity;
using KingRoad.EPM.Web.ViewModels;
namespace KingRoad.EPM.Web.Areas.Permission.Controllers
{
    /// <summary>
    /// 操作权限管理
    /// 功能描述：操作权限的增删改查、拖拽
    /// 模块负责人：苏千军
    /// 完成日期：2011.9.23
    /// </summary>
    public class PermissionItemController : Controller
    {
        //
        // GET: /Permission/PermissionItem/

        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 获取当前用户的所有操作权限
        /// </summary>
        /// <returns>json数据</returns>
        public JsonResult GetPermissionItems()
        {
            var list = ServiceManager.Instance.PermissionService.GetDT();
            return Json(list);
        }
        //详情页
        // GET: /Permission/PermissionItem/Details/5
        public ActionResult Details(int id)
        {
            Base_PermissionItem item = ServiceManager.Instance.PermissionService.GetPermissionEntityByID(id.ToString());
            ViewBag.ParentName = item.ParentId == null || item.ParentId == 0 ? "" : ServiceManager.Instance.PermissionService.GetPermissionEntityByID(item.ParentId.ToString()).FullName;
            return View(item);
        }

        /// <summary>
        /// 根据模块获取相应操作权限列表 WS 2011.11.2
        /// </summary>
        /// <param name="id"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public ActionResult GetPermissionItemsByModuleId(string id, QueryBase query)
        {
            var list = ServiceManager.Instance.PermissionService.GetDTByParent(id);
            var jqGridData = WebUtility.TojqGridData<BasePermissionItemEntity>(list, query);

            return Json(jqGridData);
        }

        /// <summary>
        /// 将可浏览模块与操作权限结合为操作权限树返回到前端
        /// </summary>
        /// <returns></returns>
        public ActionResult GetPermissionItemsTree()
        {
            return Json("");
        }

        //添加
        // GET: /Permission/PermissionItem/Create
        public ActionResult Create(int id)
        {
            ViewBag.ID = id;
            ViewBag.Flag = "Create";
            //ViewBag.ParentName = ServiceManager.Instance.PermissionService.GetPermissionEntityByID(id.ToString()).FullName;
            return View();
        }

        //添加提交处理
        // POST: /Permission/PermissionItem/Create
        [HttpPost]
        public ActionResult Creating(string pid, Base_PermissionItem item)
        {
            string statuCode = string.Empty;
            string statuMessage = string.Empty;
            string callBackTypy = string.Empty;
            if (ModelState.IsValid)//实体验证
            {
                item.ParentId = int.Parse(pid);
                if (item.Id == 0)
                {
                    string id = ServiceManager.Instance.PermissionService.AddPermission(item, out statuCode, out statuMessage);
                    if (statuCode == "OKAdd")
                        item.Id = Convert.ToInt32(id);
                    callBackTypy = "Create";
                    statuCode = statuCode == "OKAdd" ? "200" : "300";
                }
                else
                {
                    ServiceManager.Instance.PermissionService.UpdatePermission(item, out statuCode, out statuMessage);
                    callBackTypy = "Edit";
                    statuCode = statuCode == "OKUpdate" ? "200" : "300";
                }
            }
            return Json(new CallbackStatus
            {
                message = statuMessage,
                statusCode = statuCode,
                callbackType = callBackTypy,
                data = item
            });
        }

        //编辑
        // GET: /Permission/PermissionItem/Edit/5 
        public ActionResult Edit(int id)
        {
            Base_PermissionItem item = ServiceManager.Instance.PermissionService.GetPermissionEntityByID(id.ToString());
            ViewBag.ID = item.ParentId;
            //ViewBag.ParentName = item.ParentId == null || item.ParentId == 0 ? "" : ServiceManager.Instance.PermissionService.GetPermissionEntityByID(item.ParentId.ToString()).FullName; ;
            return View("Create", item);
        }

        //编辑提交处理
        // POST: /Permission/PermissionItem/Edit/5
        //[HttpPost]
        //public ActionResult Editing(Base_PermissionItem item)
        //{
        //    string statuCode = string.Empty;
        //    string statuMessage = string.Empty;
        //    if (ModelState.IsValid)//实体验证
        //    {
        //        ServiceManager.Instance.PermissionService.UpdatePermission(item, out statuCode, out statuMessage);
        //    }
        //    return Json(new KingRoad.EPM.Web.ViewModels.CallbackStatus
        //    {
        //        message = statuMessage,
        //        statusCode = statuCode == "OKUpdate" ? "200" : "300",
        //        callbackType = "Edit",
        //        data = item
        //    });
        //}

        //删除
        // GET: /Permission/PermissionItem/Delete/5 
        public ActionResult Delete(int id)
        {
            string statuCode;
            string statuMessage;
            ServiceManager.Instance.PermissionService.DeletePermissionByID(id.ToString(), out statuCode, out statuMessage);
            return Json(new CallbackStatus
            {
                message = statuMessage,
                statusCode = statuCode,
                callbackType = "closeCurrent"
            });
        }
        /// <summary>
        /// 弹出选择模块树的功能
        /// </summary>
        /// <returns></returns>
        public ActionResult SelectTree()
        {
            return View();
        }
        //拖拽操作
        public ActionResult Move(int id, int pid)
        {
            string statuCode;
            string statuMessage;
            ServiceManager.Instance.PermissionService.PermissionTO(id.ToString(), pid.ToString(), out statuCode, out statuMessage);
            Base_PermissionItem item = ServiceManager.Instance.PermissionService.GetPermissionEntityByID(id.ToString());
            return Json(new CallbackStatus
            {
                message = statuMessage,
                statusCode = statuCode,
                callbackType = "closeCurrent",
                data = item
            });
        }
        //移动
        public ActionResult MoveNode(int id)
        {
            ViewBag.Id = id;
            return View();
        }
    }
}
